﻿using System;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using HashFoo.Rest.Server;
using HashFoo.Rest.ServerTests.FakeHttpStack;
using HashFoo.Rest.ServerTests.MockResources;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace HashFoo.Rest.ServerTests
{
    internal class FakeModelBindingContext : ModelBindingContext
    {
    }

    [TestClass]
    public class ResourceModelBinderTests
    {
        [TestMethod]
        public void binds_resource_if_serializer_exists()
        {
            // Arrange
            var formCollection = new NameValueCollection
                                 {
                                     {"foo.month", "2"},
                                     {"foo.day", "12"},
                                     {"foo.year", "1964"}
                                 };

            var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
            var modelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, typeof (object));

            var bindingContext = new ModelBindingContext
                                 {
                                     ModelName = "foo",
                                     ValueProvider = valueProvider,
                                     ModelMetadata = modelMetadata
                                 };

            var binder = new ResourceModelBinder(
                new[]
                {
                    new JsonSerializerMock()
                });

            var httpContext = new FakeHttpContext {FakeRequest = {ContentType = "application/json"}};
            var controllerContext =
                new ControllerContext
                {
                    HttpContext = httpContext
                };

            using (var stream = new MemoryStream())
            {
                httpContext.FakeRequest.SetBody("", stream);

                var result = binder.BindModel(controllerContext, bindingContext);
                Assert.IsNotNull(result);
            }
        }
    }
}